package com.tjetc.dao.impl;

import com.tjetc.dao.DoctorDao;
import com.tjetc.domain.Doctor;
import com.tjetc.utils.Db;
import com.tjetc.utils.PageInfo;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;

public class DoctorDaoImpl implements DoctorDao {
    @Override
    public Doctor login(String username, String password) throws SQLException {
        return Db.runner0().query("select * from doctor where username = ? and password = ?",new BeanHandler<>(Doctor.class),
                username,password);
    }

    @Override
    public Doctor getById(int id) throws SQLException {
        return Db.runner0().query("select * from doctor where id = ?",new BeanHandler<>(Doctor.class),id);
    }

    @Override
    public Doctor findByName(String username) throws SQLException {
        return Db.runner0().query("select * from doctor where username = ?",new BeanHandler<>(Doctor.class),
                username);
    }

    @Override
    public List<Doctor> selectFifth() throws SQLException {
        return Db.runner0().query("select * from doctor where id < 6",
                new BeanListHandler<>(Doctor.class));
    }

    @Override
    public int getCount(String name) throws SQLException {
        return ((Long)Db.runner0().query("select count(id) from doctor where username like ? ",
                new ScalarHandler<>(),"%"+name+"%")).intValue();
    }

    @Override
    public List<Doctor> list(String name, PageInfo<Doctor> pageInfo) throws SQLException {
        return Db.runner0().query("select * from doctor where username like ?  limit ?,? ",
                new BeanListHandler<>(Doctor.class),"%"+name+"%",
                pageInfo.getStartRecord(),pageInfo.getPageSize());
    }

    @Override
    public void add(Doctor doctor) throws SQLException {
        Db.runner().update(Db.getConnection(),"insert into doctor(username,age,sex,phone,depid,avatar) values(?,?,?,?,?,?)",
                doctor.getUsername(),doctor.getAge(),doctor.getSex(),doctor.getPhone(),doctor.getDepid(),doctor.getAvatar());
    }

    @Override
    public int update(Doctor doctor) throws SQLException {
        return Db.runner0().update("update doctor set username=?,phone=?,sex=?,age=?,avatar=? where id = ?",
                doctor.getUsername(),doctor.getPhone(),doctor.getSex(),doctor.getAge(),
                doctor.getAvatar(),doctor.getId());
    }

    @Override
    public void del(int id) throws SQLException {
        Db.runner().update(Db.getConnection(),"delete from doctor where id=?",id);
    }

    @Override
    public List<Doctor> listAll() throws SQLException {
        return Db.runner0().query("select * from doctor",new BeanListHandler<>(Doctor.class));
    }

    @Override
    public Doctor getpwdById(int id) throws SQLException {
        return Db.runner0().query("select * from doctor where id=?",
                new BeanHandler<>(Doctor.class),id);
    }

    @Override
    public int updatepwd(Doctor doctor) throws SQLException {
        return Db.runner0().update("update doctor set password=? where id=?",
                doctor.getPassword(),doctor.getId());
    }

    @Override
    public int getDepidById(int id) throws SQLException {
        return Db.runner0().query("select depid from doctor where id = ?",new ScalarHandler<>(),id);
    }

    @Override
    public List<Doctor> queryByDepid(int depid) throws SQLException {
        return Db.runner0().query("select * from doctor where depid = ?",new BeanListHandler<>(Doctor.class),depid);
    }
}
